package com.itlong.cloud.operate.service.impl;

import com.itlong.cloud.POJO.DTO.operate.OperatePlanManageInfoDTO;
import com.itlong.cloud.POJO.DTO.operate.OperatePlanManagePageDTO;
import com.itlong.cloud.POJO.PO.OperateDevicePlanManagePO;
import com.itlong.cloud.POJO.VO.operate.OperatePlanManageInfoVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.enumerate.DataStatusEnum;
import com.itlong.cloud.operate.dao.IOperatePlanManageDao;
import com.itlong.cloud.operate.service.IOperatePlanManageService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.page.Page;
import com.itlong.cloud.utils.random.LogicIdUtil;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

/**
 * <desc>
 *      运营-智慧停车-设备方案管理服务接口实现类。
 * <desc/>
 *
 * @createDate 2018/11/05.
 */
@Service
public class IOperatePlanManageServiceImpl implements IOperatePlanManageService {

    private static final Logger LOG = LoggerFactory.getLogger(IOperatePlanManageServiceImpl.class);

    @Autowired
    IOperatePlanManageDao iOperatePlanManageDao;

    /**
     * <desc>
     *      分页获取设备方案信息。
     * <desc/>
     *
     * @param  operatePlanManagePageDTO 参数请求对象
     * @return
     * @author Qiang.S
     * @createDate 2018/11/05
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Page<OperatePlanManageInfoVO> getPlanInfoPage(OperatePlanManagePageDTO operatePlanManagePageDTO) throws Exception{
        //将DTO转换为Map params
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(operatePlanManagePageDTO);
        //分页类默认pageSize为20
        Page<OperatePlanManageInfoVO> page = new Page<>();
        page.getPage().setPageSize(operatePlanManagePageDTO.getPageSize());
        page.getPage().setCurrentPage(operatePlanManagePageDTO.getCurrentPage());
        params.put("currentPage",page.getPage().getStartSlip());
        params.put("pageSize",page.getPage().getPageSize());
        page.setRows(iOperatePlanManageDao.getPlanInfoPage(params),iOperatePlanManageDao.getPlanInfoCount(params));
        return page;
    }

    /**
     * <desc>
     *      判断方案名称是否重复。
     * <desc/>
     *
     * @param  planName 方案名称
     * @param  planId 方案编号
     * @return
     * @author Qiang.S
     * @createDate 2018/11/05
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Integer checkPlanName(String planName,String planId) throws Exception{
        return iOperatePlanManageDao.checkPlanName(planName,planId);
    }

    /**
     * <desc>
     *      保存设备方案。
     * <desc/>
     *
     * @param  operatePlanManageInfoDTO 参数请求对象
     * @return
     * @author Qiang.S
     * @createDate 2018/11/05
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Integer save(OperatePlanManageInfoDTO operatePlanManageInfoDTO) throws Exception{
        Date nowTime = new Date();
        OperateDevicePlanManagePO operateDevicePlanManagePO = new OperateDevicePlanManagePO();
        BeanUtils.copyProperties(operatePlanManageInfoDTO,operateDevicePlanManagePO);
        operateDevicePlanManagePO.setPlanId(LogicIdUtil.bussinessId());
        operateDevicePlanManagePO.setStatus(DataStatusEnum.NORMAL.getType());
        operateDevicePlanManagePO.setCreateTime(nowTime);
        return iOperatePlanManageDao.save(SqlUtil.durableData(operateDevicePlanManagePO, PlatformConstants.TABLE_SAVE));
    }

    /**
     * <desc>
     *      保存设备方案。
     * <desc/>
     *
     * @param  operatePlanManageInfoDTO 参数请求对象
     * @return
     * @author Qiang.S
     * @createDate 2018/11/05
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Integer update(OperatePlanManageInfoDTO operatePlanManageInfoDTO) throws Exception{
        return iOperatePlanManageDao.update(operatePlanManageInfoDTO.getPlanName(),operatePlanManageInfoDTO.getPlanId());
    }

    /**
     * <desc>
     *      保存设备方案。
     * <desc/>
     *
     * @param  operatePlanManageInfoDTO 参数请求对象
     * @return
     * @author Qiang.S
     * @createDate 2018/11/05
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Integer delete(OperatePlanManageInfoDTO operatePlanManageInfoDTO) throws Exception{
        return iOperatePlanManageDao.delete(operatePlanManageInfoDTO.getPlanId());
    }

    /**
     * <desc>
     *      根据方案编号获取名称
     * <desc/>
     *
     * @param planId 方案编号
     * @return
     * @author Qiang.S
     * @createDate 2018-09-25
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public OperatePlanManageInfoVO getDetail(String planId) throws Exception{
        return iOperatePlanManageDao.getDetail(planId);
    }

}
